<template>
	<view>
		<text style="color: #94bcff;font-size: 12px;width: 100%;padding-left: 5%;">
			共有 {{ cardlist.length }} 条记录</text>
		<view class="container">
			<uni-card v-for="(card, index) in cardlist" :key="index" :sub-title="card.factory" :title="card.name"
				:extra="card.post">
				<text>{{card.text}}</text>
				<view class="card-actions">
					<view class="card-actions-item" @click="actions1Click(card.text)">
						<uni-icons type="paperplane" size="18" color="#999"></uni-icons>
						<text class="card-actions-item-text">复制</text>
					</view>
					<view class="card-actions-item" @click="actions4Click(card.id)">
						<uni-icons type="clear" size="18" color="#999"></uni-icons>
						<text class="card-actions-item-text">删除</text>
					</view>
					<view class="card-actions-item" @click="actions3Click(card)">
						<uni-icons type="checkmarkempty" size="18" color="#999"></uni-icons>
						<text class="card-actions-item-text">结算</text>
					</view>
				</view>
			</uni-card>
		</view>
		<!-- 提示信息弹窗 -->
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="600"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: 'center',
				msgType: 'success',
				messageText: '这是一条成功提示',
				value: '',
				title: '',
				cardlist: '',
				titlefacotry: ''
			}
		},
		onShow() {

			this.title = uni.getStorageSync('detailstitle');
			this.titlefacotry = uni.getStorageSync('detailsfacotry');
			const datails = uni.getStorageSync('details');
			console.log("title详情页" + this.title);
			console.log("titlefacotry详情页" + this.titlefacotry);
			if (datails == 'openadmin') {
				this.api.WordGongSi({
					factory: this.titlefacotry
				}).then(res => {
					console.log(res)
					this.cardlist = res.data
					console.log(this.cardlist)
					// uni.setStorageSync('detailsfacotry','')
					if (res.data != '') {
						this.msgType = 'success'
						this.messageText = `只有这么多咯`
						this.$refs.message.open()
					} else {
						this.msgType = 'error'
						this.messageText = `没有记录哦`
						this.$refs.message.open()
					}
				})
			} else {
				this.api.WordGeRen({
					name: this.title
				}).then(res => {
					console.log(res)
					this.cardlist = res.data
					console.log(this.cardlist)
					// uni.setStorageSync('detailstitle','')
					if (res.data != '') {
						this.msgType = 'success'
						this.messageText = `只有这么多咯`
						this.$refs.message.open()
					} else {
						this.msgType = 'error'
						this.messageText = `没有记录哦`
						this.$refs.message.open()
					}
				})
			}

		},
		methods: {
			actions1Click(title) {
				// console.log("复制" + title);
				uni.setClipboardData({
					data: title,
					success() {
						uni.showToast({
							title: '已复制到剪贴板',
							icon: 'success'
						});
					},
					fail() {
						uni.showToast({
							title: '复制失败',
							icon: 'none'
						});
					}
				});
			},
			actions4Click(id) {
				console.log("删除" + id);
				this.api.SetDeleted({
					id: id
				}).then(res => {
					console.log(res)
					if (res.data.num === 1) {
						console.log("删除成功")
						this.msgType = 'success'
						this.messageText = `成功啦`
						this.$refs.message.open()
						// 从cardlist数组中删除指定的card
						for (let i = 0; i < this.cardlist.length; i++) {
							if (this.cardlist[i].id === id) {
								this.cardlist.splice(i, 1);
								break;
							}
						}
					}
				})

			},
			actions3Click(card) {
				console.log("结算" + card);
				const datails = uni.getStorageSync('details');
				if(datails == 'word'){
					this.api.SetStateById({
						id: card.id,
						name: card.name,
						text: card.text,
						factory: card.factory,
						post: card.post,
						price: card.price
					}).then(res => {
						console.log(res)
						if (res.status >= 1) {
							this.msgType = 'success'
							this.messageText = card.id + ` 结算成功啦`
							this.$refs.message.open()
							// 从cardlist数组中删除指定的card
							for (let i = 0; i < this.cardlist.length; i++) {
								if (this.cardlist[i].id === card.id) {
									this.cardlist.splice(i, 1);
									break;
								}
							}
						} else {
							this.msgType = 'error'
							this.messageText = card.id + ` 结算失败啦`
							this.$refs.message.open()
						}
					})
				}else{
					this.api.SetStateLineById({
						id: card.id,
						name: card.name,
						text: card.text,
						factory: card.factory,
						post: card.post,
						price: card.price
					}).then(res => {
						console.log(res)
						if (res.status >= 1) {
							this.msgType = 'success'
							this.messageText = card.id + ` 结算成功啦`
							this.$refs.message.open()
							// 从cardlist数组中删除指定的card
							for (let i = 0; i < this.cardlist.length; i++) {
								if (this.cardlist[i].id === card.id) {
									this.cardlist.splice(i, 1);
									break;
								}
							}
						} else {
							this.msgType = 'error'
							this.messageText = card.id + ` 结算失败啦`
							this.$refs.message.open()
						}
					})
				}
				
			}
		}
	}
</script>

<style scoped lang="scss">
	//卡片固定样式
	.container {
		overflow: auto;
		// /* 控制内容溢出时显示滚动条 */
		// margin-top: 90px;
		/* 调整合适的距离 */
		padding-bottom: 30px;

		/* 添加底部间距，防止内容紧贴底部 */
		.card-actions {
			display: flex;
			justify-content: space-between;
			padding: 0 2%;
		}

		.card-actions-item {
			display: flex;
			align-items: center;
			padding-top: 10px;

			.card-actions-item-text {
				padding-left: 5px;
			}
		}
	}
</style>